using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.InputSystem;

public static class NintendoPlayerPrefs
{
	[Serializable]
	public class SPair<T, F>
	{
		public T key;

		public F value;
	}

	private static Dictionary<string, float> floatValues = new Dictionary<string, float>();

	private static Dictionary<string, int> intValues = new Dictionary<string, int>();

	private static Dictionary<string, bool> boolValues = new Dictionary<string, bool>();

	private static Dictionary<string, string> stringValues = new Dictionary<string, string>();

	private static Dictionary<string, object> objectValues = new Dictionary<string, object>();

	private const string floatKey = "floats";

	private const string intKey = "ints";

	private const string boolKey = "bools";

	private const string stringKey = "strings";

	private const string objectKey = "object";

	private static bool init = false;

	public static bool IsInit => init;

	public static void Init(string name)
	{
		if (!init)
		{
			init = true;
		}
		Save();
	}

	private static void ApplicationQuit()
	{
		if (init)
		{
			init = false;
		}
	}

	public static void SetFloat(string key, float value)
	{
		PlayerPrefs.SetFloat(key, value);
	}

	public static void SetInt(string key, int value)
	{
		PlayerPrefs.SetInt(key, value);
	}

	public static void SetBool(string key, bool value)
	{
		PlayerPrefsSetBool(key, value);
	}
	private static void PlayerPrefsSetBool(string key, bool value)
	{
		PlayerPrefs.SetInt(key, value == false ? 0 : 1);
	}
	public static void SetString(string key, string value)
	{
		PlayerPrefs.SetString(key, value);
	}

	public static void SetObject(string key, object value)
	{
		PlayerPrefs.SetString(key, JsonConvert.SerializeObject(value));
	}

	private static byte[] ObjectToByteArray(UnityEngine.Object obj)
	{
		if (obj == null)
		{
			return null;
		}
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		MemoryStream memoryStream = new MemoryStream();
		binaryFormatter.Serialize(memoryStream, obj);
		return memoryStream.ToArray();
	}

	private static UnityEngine.Object ByteArrayToObject(byte[] arrBytes)
	{
		MemoryStream memoryStream = new MemoryStream();
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		memoryStream.Write(arrBytes, 0, arrBytes.Length);
		memoryStream.Seek(0L, SeekOrigin.Begin);
		return (UnityEngine.Object)binaryFormatter.Deserialize(memoryStream);
	}

	public static float GetFloat(string key)
	{
		return PlayerPrefs.GetFloat(key);
	}

	public static float GetFloat(string key, float fall)
	{
		return PlayerPrefs.GetFloat(key, fall);
	}

	public static int GetInt(string key)
	{
		return PlayerPrefs.GetInt(key);
	}

	public static int GetInt(string key, int fall)
	{
		return PlayerPrefs.GetInt(key, fall);
	}

	public static bool GetBool(string key)
	{
		return PlayerPrefsGetBool(key);
	}
	private static bool PlayerPrefsGetBool(string key)
	{
		return PlayerPrefs.GetInt(key, 0) != 0;
	}
	public static string GetString(string key)
	{
		return PlayerPrefs.GetString(key);
	}

	public static T GetObject<T>(string key)
	{
		return JsonConvert.DeserializeObject<T>(PlayerPrefs.GetString(key));
	}

	public static string GetString(string key, string fall)
	{
		return PlayerPrefs.GetString(key, fall);
	}

	public static object GetObject(string key, object fall)
	{
		return ToObjectFromArray(Encoding.UTF8.GetBytes(PlayerPrefs.GetString(key)));
	}

	public static void Save()
	{
	}

	public static void SaveFloats()
	{
	}

	public static void SaveInts()
	{
	}

	public static void SaveBools()
	{
	}

	public static void SaveStrings()
	{
	}

	public static void SaveObjects()
	{
	}

	public static void LoadAll()
	{
		CheckNull();
		ClearDics();
	}

	public static void DeleteKey(string key)
	{
		PlayerPrefs.DeleteKey(key);
	}

	public static void DeleteAll()
	{
		PlayerPrefs.DeleteAll();
	}

	public static bool HasKey(string key)
	{
		return PlayerPrefs.HasKey(key);
	}

	public static void CheckNull()
	{
		if (floatValues == null)
		{
			floatValues = new Dictionary<string, float>();
		}
		if (intValues == null)
		{
			intValues = new Dictionary<string, int>();
		}
		if (boolValues == null)
		{
			boolValues = new Dictionary<string, bool>();
		}
		if (stringValues == null)
		{
			stringValues = new Dictionary<string, string>();
		}
		if (objectValues == null)
		{
			objectValues = new Dictionary<string, object>();
		}
	}

	public static void ClearDics()
	{
		floatValues.Clear();
		intValues.Clear();
		boolValues.Clear();
		stringValues.Clear();
		objectValues.Clear();
	}

	private static byte[] ToByteArray(object source)
	{
		if (source == null)
		{
			return null;
		}
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		using MemoryStream memoryStream = new MemoryStream();
		binaryFormatter.Serialize(memoryStream, source);
		return memoryStream.ToArray();
	}

	private static object ToObjectFromArray(byte[] data)
	{
		if (data == null)
		{
			return null;
		}
		if (data.Length == 0)
		{
			return null;
		}
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		using MemoryStream memoryStream = new MemoryStream(data);
		Debug.Log(memoryStream.ToString());
		return binaryFormatter.Deserialize(memoryStream);
	}
}
